//
//  MainTabBarVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/20.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "MainTabBarVC.h"
#import "ConversationListVC.h"
#import "AddressListVC.h"
#import "BaseNavigationController.h"
#import "MineListVC.h"
#import "IMManager.h"
#import <WZLBadgeImport.h>
#import "AppDelegate.h"
#import "AddressGroupVC.h"
#import "FriendsContentVC.h"

@interface MainTabBarVC ()<IMManagerDelegate>

@property (nonatomic, strong) ConversationListVC *conversationVC;
@property (nonatomic, strong) FriendsContentVC *friendsContentVC;
@property (nonatomic, strong) AddressListVC *addressVC;
@property (nonatomic, strong) MineListVC *mineVC;
@property (nonatomic, strong) AddressGroupVC *groupVC;

@end

@implementation MainTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewControllers];
    [[IMManager shareManager] addIMDelegate:self];
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"new_friend_apply"] > 0
        ) {
        [self.friendsContentVC.tabBarItem showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
    }
    
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:notification_TokenFailed object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) {
        
        [kUsermanager clean];
        [[IMManager shareManager] clean];
        [kAppDelegate enterLoginVC];
        
    }];
}

- (void)setupViewControllers {
    self.conversationVC = [ConversationListVC new];
    self.conversationVC.tabBarItem.title = @"聊天";
    self.conversationVC.tabBarItem.image = UIImage.k_imgName(@"tab_conversation_normal");
    self.conversationVC.title = @"聊天";
    
    self.friendsContentVC = FriendsContentVC.new;
    self.friendsContentVC.title =
    self.friendsContentVC.tabBarItem.title = @"好友";
    self.friendsContentVC.tabBarItem.image = UIImage.k_imgName(@"tab_address_normal");

    
    self.addressVC = AddressListVC.new;
    self.addressVC.title =
    self.addressVC.tabBarItem.title = @"通讯录";
    self.addressVC.tabBarItem.image = UIImage.k_imgName(@"tab_address_normal");
    
    self.groupVC = AddressGroupVC.new;
    self.groupVC.title =
    self.groupVC.tabBarItem.title = @"分组";
    self.groupVC.tabBarItem.image = UIImage.k_imgName(@"tab_group_normal");
    
    self.mineVC = MineListVC.new;
    self.mineVC.title =
    self.mineVC.tabBarItem.title = @"我的";
    self.mineVC.tabBarItem.image = UIImage.k_imgName(@"tab_mine_normal");
    
    BaseNavigationController *nv1 = [[BaseNavigationController alloc] initWithRootViewController:self.conversationVC];
    BaseNavigationController *nv8 = [[BaseNavigationController alloc] initWithRootViewController:self.friendsContentVC];
    BaseNavigationController *nv2 = [[BaseNavigationController alloc] initWithRootViewController:self.addressVC];
    BaseNavigationController *nv3 = [[BaseNavigationController alloc] initWithRootViewController:self.groupVC];
    BaseNavigationController *nv4 = [[BaseNavigationController alloc] initWithRootViewController:self.mineVC];

    self.viewControllers = @[nv1, nv8, nv4];
    self.tabBar.tintColor = UIColor.k_t1Color;
    
}

#pragma mark immanager delegate
- (void)imManager:(IMManager *)manager receiveFriendStatusMsg:(MsgItem *)msg {
    if (msg.type == MsgType_Friend_apply) {
        [FileUtils writeObject:@"2" toUserDefaultWithKey:@"new_friend_apply"];
        [self.friendsContentVC.tabBarItem showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
    } else if (msg.type == MsgType_Friend_agree) {
//        [FileUtils writeObject:@"1" toUserDefaultWithKey:@"new_friend_agree"];
        [kUsermanager updateFriendList];
    } else if (msg.type == MsgType_Friend_delete) {
        [kUsermanager updateFriendList];
    }
}

@end
